package com.scse.bms.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.scse.bms.domain.BookCaseVO;
import com.scse.bms.domain.BookVO;
import com.scse.bms.entity.BookDO;
import com.scse.bms.entity.EBookDO;
import com.scse.bms.entity.HistoryDO;
import com.scse.bms.mapper.BookMapper;
import com.scse.bms.mapper.HistoryMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Title: BookServiceImpl
 * @Author: 吴赠禹
 * @Date: 2021/1/11 15:55
 * @Version: 1.0
 * @Description: TODO
 */

@Slf4j
@Service("bookService")
@Transactional(rollbackFor = Exception.class)
public class BookServiceImpl implements BookService {

    @Autowired
    private BookMapper bookMapper;

    @Autowired
    private HistoryMapper historyMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addEBook(EBookDO book) {
        return bookMapper.insertEBook(book);
    }

    @Override
    public int addBook(BookDO book) {
        return bookMapper.insertBook(book);
    }

    @Override
    public List<BookVO> selectAll(int page, int limit) {
        return bookMapper.selectAll(page, limit);
    }

    @Override
    public int countAll() {
        return bookMapper.countAll();
    }

    @Override
    public BookDO selectBook(Integer id) {
        return bookMapper.selectById(id);
    }

    @Override
    public int updateBook(BookDO book) {
        return bookMapper.updateById(book);
    }

    @Override
    public int removeBook(BookDO book) {
        return bookMapper.deleteById(book);
    }

    @Override
    public int countByCase(BookCaseVO book) {
        return bookMapper.countByCase(book);
    }

    @Override
    public List<BookVO> getBookByCase(BookCaseVO book) {
        return bookMapper.getAllByCase(book);
    }

    @Override
    public List<BookVO> countBookByCase(BookCaseVO book) {
        return bookMapper.countAllByCase(book);
    }

    @Override
    public List<BookVO> getBookWithRecommend(int count) {
        return bookMapper.getRecommended(count);
    }

    @Override
    public List<BookVO> getBookWithRecently(int count) {
        return bookMapper.getRecentBook(count);
    }

    @Override
    public List<BookVO> showAuthors(int count) {
        return bookMapper.getAuthor(count);
    }

    @Override
    public IPage<BookDO> selectBookPage(Page<BookDO> page, Integer themeId) {
        return bookMapper.selectAllByThemeId(page, themeId);
    }

    @Override
    public IPage<BookVO> selectBookPageByTitleOrAuthor(Page<BookVO> page, String keyword) {
        return bookMapper.selectAllByTitleOrAuthor(page, keyword);
    }

    @Override
    public int saveHistory(HistoryDO history) {
        return historyMapper.insert(history);
    }

    @Override
    public int saveHistory2(Integer userId, Integer bookId, Integer themeId) {
        return historyMapper.insert(userId, bookId, themeId);
    }

    @Override
    public List<BookVO> getNewBookWithThemeId(Integer themeId, Integer limit) {
        return bookMapper.selectRecentBookByThemeId(themeId, limit);
    }

    @Override
    public List<BookVO> getNewBookWithUploadTimeAndDisorder(Integer limit) {
        return bookMapper.selectBookByUploadTimeWithRand(limit);
    }

    @Override
    public List<BookVO> getBookWhichMostVisited(Integer limit) {
        return bookMapper.selectBookWhichMostVisited(limit);
    }

    @Override
    public List<BookVO> getBookWhichMostDownload(Integer limit) {
        return bookMapper.selectBookWhichMostDownload(limit);
    }
}
